<?php
namespace Lonicera\core;
class Route{
	//定义路由成员属性
	public $group;
	public $control;
	public $action;
	public $params;
	
	public function __construct(){
		
		
	}
	
	public function init(){
		$route = $this->getRequest();
		$this->group = $route['group'];
		$this->control = $route['controll'];
		$this->action = $route['action'];
		!empty($route['params'])&&$this->params = $route['params'];
	}
	
	//在这里可以对不同样式的URL进行分门别类的处理,目前只实现了对PathUrl方式的和传统URL方式的解析
	public function getRequest(){
		//return $this->parseTradition(); //这是传统URL
		//下面代码为PATH_INFO
		$filter_param = array('<','>','"',"'",'%3C','%3E','%22','%27','%3c','%3e'); //过滤一些特殊转义字符
		$uri = str_replace($filter_param,'',$_SERVER['REQUEST_URI']);
		$path = parse_url($uri); //分割访问链接parse_url可设定第二参数返回特定部分的内容PHP_URL_SCHEME, PHP_URL_HOST, PHP_URL_PORT, PHP_URL_USER, PHP_URL_PASS, PHP_URL_PATH, PHP_URL_QUERY or PHP_URL_FRAGMENT.
		if(strpos($path['path'], 'index.php') == 0){ //查找某个字符第一次出现的位置,返回位置的
			$urlR0 = $path['path']; //处理urlrewrite后的Url,此时不带index.php
		}else{
			$urlR0 = substr($path['path'], strlen('index.php')+1);
		}
		
		$urlR = ltrim($urlR0, '/'); //移除左边的/,得到最需要的URL
		if($urlR == ''){
			$route = $this->parseTradition();
			return $route;
		}
		$reqArr = explode('/', $urlR); //字符串转数组
		//现在需要去除空白, 比如 index.php/g  // b,多个斜杠产生的空数组
		foreach($reqArr as $key=>$value){
			if(empty($value)){
				unset($reqArr[$key]);
			}
		}
		
		$cnt = count($reqArr); //获取元素个数
		if(empty($reqArr) || empty($reqArr[0])){
			$cnt = 0;
		}
		switch($cnt){
			case 0:
				$route['controll'] = $GLOBALS['_config']['defaultController'];
				$route['action'] = $GLOBALS['_config']['defaultAction'];
				$route['group'] = $GLOBALS['_config']['defaultApp'];
				break;
			case 1:
				if(stripos($reqArr[0], ':')){
					$gc = explode(':',$reqArr[0]);
					$route['controll'] = $gc[1];
					$route['action'] = $GLOBALS['_config']['defaultAction'];
					$route['group'] = $gc[0];
				}
				else{
					$route['controll'] = $reqArr[0];
					$route['action'] = $GLOBALS['_config']['defaultAction'];
					$route['group'] = $GLOBALS['_config']['defaultApp'];
				}
				break;
			default:
				if(stripos($reqArr[0], ':')){
					$gc = explode(':',$reqArr[0]);
					$route['controll'] = $gc[1];
					$route['action'] = $reqArr[1];
					$route['group'] = $gc[0];
				}
				else{
					$route['controll'] = $reqArr[0];
					$route['action'] = $reqArr[1];
					$route['group'] = $GLOBALS['_config']['defaultApp'];
				}
				
				for($i=2;$i<$cnt;$i++){
					$route['param'][$reqArr[$i]] = isset($reqArr[++$i])?$reqArr[$i]:''; //组装参数
				}
				break;
		}
		//需要处理query字符串了
		
		if(!empty($path['query'])){
			parse_str($path['query'], $routeQ);//把查询字符串解析到变量中：
			if(empty($route['param'])){
				$route['param'] = array();
			}
			$route['param'] += $routeQ;
		}
		
		//注意. 需要初始化数组得param数组
		return $route;
	}
	
	//层层往下走,缺什么补什么
	/*
	* 对传统URL参数模式的访问地址进行解析,提取出里面的group,controller,action,param这4个参数,这4个参数会传给bootstrap的dispatch进行分发处理
	*/
	public function parseTradition(){
		
		$route = [];
		if(!isset($_GET[$GLOBALS['_config']['UrlGroupName']])){ //$GLOBALS包含所有的变量
			$_GET[$GLOBALS['_config']['UrlGroupName']] = '';
		}
		
		if(!isset($_GET[$GLOBALS['_config']['UrlControllerName']])){
			$_GET[$GLOBALS['_config']['UrlControllerName']] = '';
		}
		
		if(!isset($_GET[$GLOBALS['_config']['UrlActionName']])){
			$_GET[$GLOBALS['_config']['UrlActionName']] = '';
		}
		$route['group'] = $_GET[$GLOBALS['_config']['UrlGroupName']];
		$route['controll'] = $_GET[$GLOBALS['_config']['UrlControllerName']];
		$route['action'] = $_GET[$GLOBALS['_config']['UrlActionName']];
		//初始化之后释放变量
		unset($_GET[$GLOBALS['_config']['UrlGroupName']]);
		unset($_GET[$GLOBALS['_config']['UrlControllerName']]);
		unset($_GET[$GLOBALS['_config']['UrlActionName']]);
		$route['param'] = $_GET;//如果要获得原始的数据,可以使用$_REQUEST 为了获取访问携带的参数 如果使用$_REQUEST会同时获取到$_GET和$_POST的数据
		//缺省参数赋值
		if($route['group'] == NULL){
			$route['group'] = $GLOBALS['_config']['defaultApp'];//设定路由返会的数据
		}
		if($route['controll'] == NULL){
			$route['controll'] = $GLOBALS['_config']['defaultController']; //_config是变量
		}
		if($route['action'] == NULL){
			$route['action'] = $GLOBALS['_config']['defaultAction'];
		}
		
		return $route;
	}
	 
}

?>